package com.bricker.laboratorymanagement.business.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bricker.laboratorymanagement.business.dao.LaboratoryMapper;
import com.bricker.laboratorymanagement.business.entity.Experiment;
import com.bricker.laboratorymanagement.business.entity.Laboratory;
import com.bricker.laboratorymanagement.business.model.LabSelectModel;
import com.bricker.laboratorymanagement.business.service.ExperimentService;
import com.bricker.laboratorymanagement.business.service.LaboratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LaboratoryServiceImpl extends ServiceImpl<LaboratoryMapper, Laboratory> implements LaboratoryService{
    @Autowired
    private LaboratoryMapper laboratoryMapper;
    @Autowired
    private ExperimentService experimentService;
    @Override
    public Page<Laboratory> getLabByPage(LabSelectModel labSelectModel) {
        Page<Laboratory> page =new Page<>(labSelectModel.getCurrent(),labSelectModel.getSize());
        List<Laboratory> laboratories =laboratoryMapper.getLabByPage(page,labSelectModel);
//        Map<Long,String> map =getEperimentMap(laboratories);
//        if(CollectionUtils.isNotEmpty(laboratories)){
//            for (Laboratory laboratory : laboratories) {
//                String exIds= laboratory.getExIds();
//                List<Long> longs = Arrays.asList(exIds.split(",")).stream().map(x->Long.valueOf(x)).collect(Collectors.toList());
//                List<String> list=new ArrayList<>();
//                for (Long aLong : longs) {
//                    String exName=map.get(aLong);
//                    list.add(exName);
//                }
//                String exNames=String.join(",",list);
//                laboratory.setExNames(exNames);
//            }
//        }
        return page.setRecords(laboratories);
    }

    @Override
    public void saveOrUpdateLab(Long uId,List<Laboratory> laboratories) {
        for (Laboratory laboratory : laboratories) {
            if(laboratory.getId()==null){
                laboratory.setCreatedBy(uId);
                laboratory.setCreatedTime(LocalDateTime.now());
                laboratory.setDelFlag("0");
                laboratory.setLabCruNum(0);
                laboratory.setLabStatus("0");
            }
            laboratory.setUpdatedBy(uId);
            laboratory.setUpdatedTime(LocalDateTime.now());
        }
        super.saveOrUpdateBatch(laboratories);
    }

    @Override
    public void deleteLab(Long uId, List<Long> ids) {
        List<Laboratory> laboratories =super.list(Wrappers.<Laboratory>lambdaQuery().in(Laboratory::getId,ids))
                .stream().map(x->{
                    x.setDelFlag("1");
                    return x;
                }).collect(Collectors.toList());
        super.updateBatchById(laboratories);
    }

    public Map<Long,String> getEperimentMap(List<Laboratory> laboratories){
        if(CollectionUtils.isNotEmpty(laboratories)){
            List<Long> ids =new ArrayList<>();
            for (Laboratory laboratory : laboratories) {
                String exIds= laboratory.getExIds();
                List<Long> longs = Arrays.asList(exIds.split(",")).stream().map(x->Long.valueOf(x)).collect(Collectors.toList());
                ids.addAll(longs);
            }
            List<Long> newIds=ids.stream().distinct().collect(Collectors.toList());
            List<Experiment> experiments =experimentService.list(Wrappers.<Experiment>lambdaQuery().in(Experiment::getId,newIds));
            Map<Long,String> map=experiments.stream().collect(Collectors.toMap(Experiment::getId,Experiment::getExName));
            return  map;
        }
        return null;
    }
}
